﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Collections.Specialized;
using SyCODE.Component.Security;
using SyCODE.Component.DataFactory;
using SyCODE.Component;
using System.Xml;
using System.IO;
using System.Data.Common;

namespace SyCODE.Blog
{
    /// <summary>
    /// GetStore 的摘要说明
    /// </summary>
    public class GetStore : IHttpHandler
    {
        #region IHttpHandler 成员

        public bool IsReusable
        {
            get
            {
                return true;
            }
        }

        public void ProcessRequest(HttpContext context)
        {
            NameValueCollection nvc = new NameValueCollection(context.Request.QueryString);
            DBAM dbam = new DBAM();
            try
            {
                dbam.BuildParameters("store", nvc);

                context.Response.Expires = 120;
                context.Response.ContentType = "text/xml";

                XmlTextWriter writer = new XmlTextWriter(context.Response.OutputStream, context.Response.ContentEncoding);

                writer.WriteStartDocument(); 
                writer.WriteProcessingInstruction("xml-stylesheet", "type='text/xsl' href='/ui/common/store.xsl'");
                
                writer.WriteStartElement("html");
                writer.WriteAttributeString("u", nvc["u"]);

                writer.WriteStartElement("head");

                writer.WriteStartElement("meta");
                writer.WriteAttributeString("http-equiv", "Content-Type");
                writer.WriteAttributeString("content", "text/html; charset=" + context.Response.ContentEncoding.WebName);
                writer.WriteEndElement();

                writer.WriteStartElement("title");

                int d = 0;
                Int32.TryParse(nvc["d"], out d);

                writer.WriteString(String.Format(StringResource.GetField("storetitle"), nvc["u"], nvc["y"] + "/" + nvc["m"] + (d > 0 ? "/" + d.ToString() : "")) + " - Powered By X3BLOG");
                writer.WriteEndElement();

                writer.WriteEndElement();

                writer.WriteStartElement("body");

                string url = "/" + nvc["u"] + "/topic/";

                DbDataReader dr = dbam.ExecuteReader();
                while (dr.Read())
                {
                    long id = dr.GetInt64(0);
                    int comment = dr.GetInt32(4);
                    int trackback = dr.GetInt32(5);
                    writer.WriteStartElement("p");

                    writer.WriteAttributeString("vc", dr.GetInt32(3).ToString());
                    writer.WriteAttributeString("c", comment.ToString());
                    writer.WriteAttributeString("t", trackback.ToString());
                    writer.WriteAttributeString("l", dr.GetDateTime(6).ToString(StringResource.GetField("datetimeformat")));

                    writer.WriteStartElement("a");
                    writer.WriteAttributeString("href", url + id.ToString());
                    writer.WriteString(dr.GetString(1));
                    writer.WriteEndElement();

                    if (comment > 0)
                    {
                        writer.WriteStartElement("a");
                        writer.WriteAttributeString("href", url + id.ToString() + "/" + ((int)Math.Ceiling(comment / 30.0f)).ToString() + ".xml");
                        writer.WriteString("comment");
                        writer.WriteEndElement();
                    }
                    if (trackback > 0)
                    {
                        writer.WriteStartElement("a");
                        writer.WriteAttributeString("href", url + id.ToString() + "/trackback.xml");
                        writer.WriteString("trackback");
                        writer.WriteEndElement();
                    }
                    writer.WriteStartElement("p");
                    writer.WriteString(dr.GetString(2));
                    writer.WriteEndElement();

                    writer.WriteEndElement();
                }
                dr.Close();

                writer.WriteEndElement();

                writer.WriteEndElement();
                writer.WriteEndDocument();
                writer.Flush();
            }
            finally
            {
                dbam.Dispose();
            }
        }

        #endregion
    }
}